/**
 * Free software.
 */

package org.nanhill.commons.pattern.search;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-10-27
 */
public abstract class AbstractSearchVisitor implements SearchPageVisitor {
	protected int start;

	protected int size;

	public AbstractSearchVisitor() {
	}

	/**
	 * @param session
	 * @param entityClz
	 */
	public AbstractSearchVisitor(int start, int size) {
		this.start = start;
		this.size = size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void doSearch() throws SearchException {
		buildQuery();
		doInternalSearch();
	}

	public void addCriterion(SearchCriterion criterion) throws SearchException {
		criterion.accept(this);
	}

	/**
	 * Adds wildcard prefix and suffix to a text string.
	 * 
	 * @param queryText
	 *            The text to be extended.
	 * @return The original text with wildcard prefix and suffix added.
	 */
	protected static String addWildcards(String queryText) {
		return "%" + queryText + "%";
	}

	/**
	 * Build search query before doInternalSearch().
	 * 
	 * @throws SearchException
	 */
	protected abstract void buildQuery() throws SearchException;

	/**
	 * Internal Search after buildeQuery()
	 * 
	 * @throws SearchException
	 */
	protected abstract void doInternalSearch() throws SearchException;

}
